package com.edu.dao;

import com.edu.data.Admin;
import com.edu.data.ClassInfo;
import com.edu.data.Contact;
import com.edu.data.Page;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Repository;
import java.util.List;

@Repository
public class MyBatisDaoImpl implements Dao {
    static SqlSession sqlSession=MyBatisUtil.createSession();
    ContactMapper contactMapper=sqlSession.getMapper(ContactMapper.class);
    AdminMapper adminMapper=sqlSession.getMapper(AdminMapper.class);
    ClassInfoMapper classInfoMapper=sqlSession.getMapper(ClassInfoMapper.class);

    @Override
    public void addContact(Contact contract) {
        contactMapper.insertSelective(contract);
    }
    @Override
    public ClassInfo getClassInfo(int classId) {
        ClassInfo classInfo=classInfoMapper.selectByPrimaryKey(classId);
        return classInfo;
    }

    @Override
    public long getContactsCount() {
        long i;
        i=contactMapper.selectCount();
        return i;
    }

    @Override
    public List<Contact> getAllContacts() {
        List<Contact> contactList=contactMapper.selectAll();
        return contactList;
    }

    @Override
    public List<Contact> getAllContactsByPage(Page page) {
        List<Contact> contactList=contactMapper.selectByPage(page);
        return contactList;
    }

    @Override
    public Admin login(String account, String pasword) {
        Admin admin=adminMapper.selectLogin(account,pasword);
        return admin;
    }

    @Override
    public void deleteContact(int id) {
        contactMapper.deleteByPrimaryKey(id);
        sqlSession.commit();
    }

    @Override
    public Contact getContact(int id) {
        Contact contact=contactMapper.selectByPrimaryKey(id);
        return contact;
    }

    @Override
    public void updateContact(Contact contact) {
        contactMapper.updateByPrimaryKeySelective(contact);
    }

    public static void main(String[] args) {
        MyBatisDaoImpl dao=new MyBatisDaoImpl();
        long cout=dao.getContactsCount();
        Page page=new Page(32,0,5);
        List<Contact> contactList=dao.getAllContactsByPage(page);
        System.out.println("contract表的记录条数为:"+contactList.size());

        dao.deleteContact(8);
        sqlSession.commit();

    }
}
